package CoteClient;

import javax.sound.sampled.LineEvent;
import javax.sound.sampled.LineListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.filechooser.FileFilter;
import javax.swing.Action;
import javax.swing.AbstractAction;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JSeparator;
import javax.swing.JButton;
import javax.swing.SwingUtilities;

import CoteClient.AudioPlayer;
import javax.swing.SwingConstants;

public class VueDiscussionLocuteur extends Vue {

	private Thread audioPlayerThread;
	private Thread audioRecorderThread;
	private AudioPlayer player;
	private Audio recorder;
	private JFileChooser chooser;
	private File audioRecordFile;
	private File audioPlayFile;
	private JButton btnFermer;
	private JButton btnEnvoyer;
	private JButton btnTerminer;
	private JButton btnLecture;
	private JButton btnArrt;
	private JButton btnEnregistrer;
	private JFrame frame;
	private Controleur controleur;

	public VueDiscussionLocuteur(final Controleur controleur, final String profileEnvoi) {
		super(controleur);
		this.controleur = controleur;
		this.setTitle("Discussion");
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setBounds(100, 100, 628, 511);

		player = new AudioPlayer();
		recorder = new Audio();

		chooser = new JFileChooser();
		chooser.setFileFilter(new FileFilter() {

			public boolean accept(File f) {
				if (f.getName().endsWith(".wav"))
					return true;
				else
					return false;
			}
			public String getDescription() {
				return null;
			}
		});

		getContentPane().setLayout(null);

		JMenuBar menuBar = new JMenuBar();
		menuBar.setBounds(0, 0, 620, 21);
		getContentPane().add(menuBar);

		JMenu mnErim = new JMenu("Menu");
		menuBar.add(mnErim);

		JMenuItem menuItem = new JMenuItem("Quitter");
		mnErim.add(menuItem);

		JMenu mnNewMenu = new JMenu("Indentification");
		menuBar.add(mnNewMenu);

		JMenuItem mntmLogin = new JMenuItem("Login");
		mntmLogin.setEnabled(false);
		mnNewMenu.add(mntmLogin);

		JMenuItem mntmLogiout = new JMenuItem("Logout");
		mntmLogiout.setEnabled(false);
		mnNewMenu.add(mntmLogiout);

		JMenuItem mntmSignUp = new JMenuItem("Sign up");
		mnNewMenu.add(mntmSignUp);

		JScrollPane scrollPaneChat = new JScrollPane();
		scrollPaneChat.setBounds(10, 33, 449, 317);
		getContentPane().add(scrollPaneChat);

		JTextArea textAreaChat = new JTextArea();
		textAreaChat.setEditable(false);
		scrollPaneChat.setViewportView(textAreaChat);

		JScrollPane scrollPaneMessage = new JScrollPane();
		scrollPaneMessage.setBounds(12, 362, 447, 66);
		getContentPane().add(scrollPaneMessage);

		final JTextArea textArea = new JTextArea();
		scrollPaneMessage.setViewportView(textArea);

		JSeparator separator = new JSeparator();
		separator.setBounds(465, 149, 143, 2);
		getContentPane().add(separator);

		btnTerminer = new JButton("Terminer");
		btnTerminer.setHorizontalAlignment(SwingConstants.LEFT);
		btnTerminer.setIcon(new ImageIcon(
				"/users/info/etu-s3/pierroa/Java/Son/src/stop.png"));
		btnTerminer.setBounds(465, 216, 149, 42);
		getContentPane().add(btnTerminer);
		btnTerminer.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {

				// if (player != null && recorder.getLine() == null) {
				// player.stop();
				// player.getLine().close();
				// }
				// else
				if (recorder.getLine() != null && player != null) {
					recorder.getLine().stop();
					recorder.getLine().close();
					JOptionPane
							.showMessageDialog(null, "Enregistrement réussi");
				} else {
					JOptionPane.showMessageDialog(null,
							"Aucune lecture ou enregistrement en cours...");
				}

				// TODO TESTER CETTE LIGNE ==>
				// Thread.currentThread().interrupt();
			}
		});

		btnEnvoyer = new JButton("Envoyer");
		btnEnvoyer.setBounds(10, 432, 149, 42);
		getContentPane().add(btnEnvoyer);
		btnEnvoyer.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
					controleur.envoiMess(textArea.getText(), profileEnvoi);
				
			}
			});

		btnLecture = new JButton("Lecture");
		btnLecture.setHorizontalAlignment(SwingConstants.LEFT);
		btnLecture.setIcon(new ImageIcon(
				"/users/info/etu-s3/pierroa/Java/Son/src/play.png"));
		btnLecture.setBounds(465, 33, 149, 40);
		getContentPane().add(btnLecture);
		btnLecture.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {

				// player.setFile(audioPlayFile);
				player.init();
				audioPlayerThread = new Thread(player);
				audioPlayerThread.start();
				player.getLine().addLineListener(new LineListener() {

					public void update(LineEvent le) {
						if (le.getType() == LineEvent.Type.STOP
								|| le.getType() == LineEvent.Type.CLOSE) {
							btnLecture.setEnabled(true);
							btnEnregistrer.setEnabled(true);
							btnArrt.setEnabled(false);
							btnTerminer.setEnabled(false);

						}
						if (le.getType() == LineEvent.Type.START) {
							btnLecture.setEnabled(false);
							btnEnregistrer.setEnabled(false);
							btnArrt.setEnabled(true);
							btnTerminer.setEnabled(false);
						}
					}
				});
			}

		});

		btnArrt = new JButton("Arrêt");
		btnArrt.setHorizontalAlignment(SwingConstants.LEFT);
		btnArrt.setIcon(new ImageIcon(
				"/users/info/etu-s3/pierroa/Java/Son/bin/stop.png"));
		btnArrt.setBounds(465, 85, 149, 40);
		getContentPane().add(btnArrt);
		btnArrt.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {

				if (player != null
						&& (recorder.getLine() == null || !recorder.getLine()
								.isActive())) {
					player.stop();
					player.getLine().close();
				}
				// else if (recorder.getLine() != null && player == null) {
				// recorder.getLine().stop();
				// recorder.getLine().close();
				// JOptionPane
				// .showMessageDialog(null, "Enregistrement réussi");
				// }
				else {
					JOptionPane.showMessageDialog(null,
							"Aucune lecture ou enregistrement en cours...");
				}

				// TODO TESTER CETTE LIGNE ==>
				// Thread.currentThread().interrupt();
			}
		});

		btnEnregistrer = new JButton("Enregistrer");
		btnEnregistrer.setHorizontalAlignment(SwingConstants.LEFT);
		btnEnregistrer.setIcon(new ImageIcon(
				"/users/info/etu-s3/pierroa/Java/Son/src/record.png"));
		btnEnregistrer.setBounds(465, 164, 149, 40);
		getContentPane().add(btnEnregistrer);
		btnEnregistrer.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int returnVal = chooser.showSaveDialog(frame);
				if (returnVal == JFileChooser.APPROVE_OPTION) {

					audioRecordFile = chooser.getSelectedFile();
					recorder.setFile(audioRecordFile);
					recorder.init();

					audioRecorderThread = new Thread(recorder);
					audioRecorderThread.start();
					recorder.getLine().addLineListener(new LineListener() {

						public void update(LineEvent le) {
							if (le.getType() == LineEvent.Type.STOP
									|| le.getType() == LineEvent.Type.CLOSE) {
								btnEnregistrer.setEnabled(true);
								btnLecture.setEnabled(true);
								btnArrt.setEnabled(false);
								btnTerminer.setEnabled(false);

							}
							if (le.getType() == LineEvent.Type.START) {
								btnLecture.setEnabled(false);
								btnEnregistrer.setEnabled(false);
								btnArrt.setEnabled(false);
								btnTerminer.setEnabled(true);
							}
						}
					});

				} else
					return;

			}
		});

		// TODO AVOIR COMMENT METTRE UN CHEMIN PREDEFINI
		// btnEnregistrer.addActionListener(new ActionListener() {
		// public void actionPerformed(ActionEvent e) {
		// audioRecordFile = chooser.getSelectedFile();
		// recorder.setFile(audioRecordFile);
		// recorder.initEnregistrement();
		// }
		// });

		JButton btnCharger = new JButton("Charger");
		btnCharger.setBounds(465, 371, 149, 25);
		getContentPane().add(btnCharger);
		btnCharger.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int returnVal = chooser.showOpenDialog(frame);
				if (returnVal == JFileChooser.APPROVE_OPTION) {
					audioPlayFile = chooser.getSelectedFile();
					player.setFile(audioPlayFile);
					player.init();

				}
			}

		});

		btnFermer = new JButton("Fermer");
		btnFermer.setBounds(465, 432, 149, 42);
		getContentPane().add(btnFermer);
		btnFermer.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//
				getControleur().lancerMenu();
			}
		});

	}// Fin VueAudio

	public void release() {
		synchronized (this) {
			this.notify();
		}
	}

}